/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package utils;

import baiduMapPojo.APIParameter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import mbean.admin.attendance.AttendanceCreateMB;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 *
 * @author dingnangen
 */
public class HttpmethodUtil {

    public static String updateEntity(String eName,String key, String value) {
        System.out.println(eName+key+value);
        try {
            OkHttpClient client = new OkHttpClient();
            String url = APIParameter.upDateEntityURL;
            FormBody formBody = new FormBody.Builder()
                    .add(key, value)
                    .add("ak", APIParameter.ak)
                    .add("service_id", APIParameter.service_id)
                    .add("entity_name", eName)
                    .build();

            Request request = new Request.Builder()
                    .url(url)
                    .post(formBody)
                    .build();

            Response response = client.newCall(request).execute();
            String result = response.body().string();
            System.out.println("response:" + result);
            return result;
        } catch (IOException ex) {

            Logger.getLogger(AttendanceCreateMB.class.getName()).log(Level.SEVERE, null, ex);
            return "failed";
        }
    }
    
    public static String addEntity(String eName,String uname) {
        System.out.println(eName );
        try {
            OkHttpClient client = new OkHttpClient();
            String url = APIParameter.addEntityURL;
            FormBody formBody = new FormBody.Builder()
                    
                    .add("ak", APIParameter.ak)
                    .add("service_id", APIParameter.service_id)
                    .add("entity_name", eName)
                    .add("username", uname)
                    .build();

            Request request = new Request.Builder()
                    .url(url)
                    .post(formBody)
                    .build();

            Response response = client.newCall(request).execute();
            String result = response.body().string();
            System.out.println("response:" + result);
            return result;
        } catch (IOException ex) {

            Logger.getLogger(AttendanceCreateMB.class.getName()).log(Level.SEVERE, null, ex);
            return "failed";
        }
    }
}
